//----------------------------------------------------------------------------
// M A R I T I M E  R E S E A R C H  I N S T I T U T E  N E T H E R L A N D S
//----------------------------------------------------------------------------
// Copyright (C) 2012 - MARIN - All rights reserved - http://www.marin.nl
//----------------------------------------------------------------------------
// Program    : mermaid
// Module     : vbm
// File       : wavebreaking_kernels.h
// Author     : M.A. de Jong
//----------------------------------------------------------------------------

#ifndef WAVEBREAKING_KERNELS_H
#define WAVEBREAKING_KERNELS_H

#ifdef USEDOUBLE
#define ONE 1.0
#else
#define ONE 1.0f
#endif

namespace wavescomputer
{
    
template <class T>
__device__ T square(const T value)
{
    return  value * value;
}

template <class T>
__global__ void kernel_wavebreaking(T* zeta_std,
                                    T* alfa_brk,
                                    const T* zeta,
                                    const SHORT* wet,
                                    const T* h0,
                                    const T zeta_mean,
                                    const T tau_brk,
                                    const T dt,
                                    const int ld)
{
    int u = bx * Bx + tx;
    int v = by * By + ty;
    int tid = v * ld + u;

    T eps_brk = dt / tau_brk;
    T one_minus_eps_brk = 1.0 - eps_brk;
    T brk = eps_brk / tau_brk;

    zeta_std[tid] = sqrt(one_minus_eps_brk * square(zeta_std[tid]) +
                         eps_brk * square(zeta[tid] - zeta_mean));
    alfa_brk[tid] *= one_minus_eps_brk;
    if (wet[tid] > 0 && (zeta_std[tid] > 0.18 * (h0[tid] + zeta_mean)))
    {
        alfa_brk[tid] += brk;
    }
 
}

} // namespace wavescomputer

#endif // WAVEBREAKING_KERNELS_H

